package com.sporadic.desk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sporadic.desk.dao.CouponDao;
import com.sporadic.desk.pojo.Coupon;
import com.sporadic.desk.pojo.UserCoupon;
import com.sporadic.desk.service.CouponService;
import dto.CouponDTO;
import dto.CouponStatusDTO;
import dto.CouponTypeDTO;
import dto.UserCouponDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: sporadic
 * @description:
 * @author: LHD   QQ:1727865856
 * @create: 2019-12-02 11:57
 **/
@Service
public class CouponServiceImpl extends ServiceImpl<CouponDao, Coupon> implements CouponService {
    @Autowired
    private CouponDao couponDao;

    @Override
    public List<CouponDTO> queryByPhoneUserCoupon(String phone, Integer mid) {
        List<CouponDTO> coupons =new ArrayList<>();
        try{
            List<Coupon> list = couponDao.queryByPhoneUserCoupon(phone, mid);
            for (Coupon coupon : list) {
                CouponStatusDTO couponStatusDTO=new CouponStatusDTO();
                BeanUtils.copyProperties(coupon.getCouponStatus(),couponStatusDTO);
                CouponTypeDTO couponTypeDTO=new CouponTypeDTO();
                BeanUtils.copyProperties(coupon.getCouponType(),couponTypeDTO);
                UserCouponDTO userCouponDTO=new UserCouponDTO();
                BeanUtils.copyProperties(coupon.getUserCoupon(),userCouponDTO);
                CouponDTO couponDTO=new CouponDTO();
                BeanUtils.copyProperties(coupon,couponDTO);
                couponDTO.setUserCouponDTO(userCouponDTO);
                couponDTO.setCouponStatus(couponStatusDTO);
                couponDTO.setCouponType(couponTypeDTO);
                coupons.add(couponDTO);
            }
            return coupons;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("网络异常!");
        }

    }
}
